import time
from NetModule import NetModule


metric_list = [
    'bytes_in',
    'bytes_out',
    'packets_in',
    'packets_out'
]

class XenNetModule(NetModule):

    def update(self):
        if self.info.has_key('netdev'):
            self.info['last_netdev'] = self.info['netdev']
            self.info['netdev'] = {}

        f = open('/proc/net/dev', 'r')
        self.set_timestamp('netdev', time.time())
        rbi = rbo = rpi = rpo = 0
        for line in f:
            line = line.strip().split(':', 1)
            if not line[0].startswith('peth'):
                continue
            data = line[1].split()
            rbi += int(data[0])
            rpi += int(data[1])
            rbo += int(data[8])
            rpo += int(data[9])

        self.set_val(('netdev', 'realbytesin'), rbi, True)
        self.set_val(('netdev', 'realbytesout'), rbo, True)
        self.set_val(('netdev', 'realpacketsin'), rpi, True)
        self.set_val(('netdev', 'realpacketsout'), rpo, True)

        if not self.info.has_key('last_netdev'):
            self.info['last_netdev'] = self.info['netdev']
